<?php

namespace App\Admin\Controllers;

use App\Models\Trip;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\User;
use Illuminate\Support\Facades\DB;

class TripController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '行程管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Trip());

        $grid->column('id', __('ID'));
        $grid->column('user_id', __('用户id'));
        $grid->column('user_name',__('用户昵称'))->display(function () {
            $res = User::find($this->user_id);
            if ($res){
                $name = $res->name;
            }else{
                $name ="";
            }

            return $name;
        });
        //$grid->column('start_addr', __('开始城市'));
        $grid->column('start',__('开始城市'))->display(function () {

            return $this->start_province." ".$this->start_city." ".$this->start_area;
        });
        $grid->column('end',__('结束城市'))->display(function () {

            return $this->end_province." ".$this->end_city." ".$this->end_area;
        });
        //$grid->column('end_addr', __('结束城市'));
        $grid->column('选择类型')->display(function () {
            if ($this->select_type==1){
                return "<span style='color:green'>人找车</span>";
            }
            if ($this->select_type==2){
                return "<span style='color:green'>车找人</span>";
            }

        });
        $grid->column('trip_date', __('行程时间'));
        $grid->column('trip_price', __('行程价格'));
        $grid->column('trip_num', __('行程人数'));
        $grid->column('car_code', __('车牌号'));
        $grid->column('是否包车')->display(function () {
            if ($this->is_charter==1){
                return "<span style='color:green'>是</span>";
            }
            if ($this->is_charter==0){
                return "<span style='color:coral'>否</span>";
            }

        });
        $grid->column('查看数量')->display(function () {

            $count = DB::table("trip_read_log")->where('trip_id',$this->id)->count();
                return $count;

        });
        $grid->column('状态')->display(function () {
            if ($this->status==1){
                return "<span style='color:green'>已通过审核</span>";
            }
            if ($this->status==0){
                return "<span style='color:#666'>待审核</span>";
            }
            if ($this->status==2){
                return "<span style='color:coral'>非通过审核</span>";
            }

        });
        $grid->column('contacter', __('联系人'));
        $grid->column('moblie', __('联系电话'));
        $grid->column('remark', __('备注'));
        $grid->column('created_at', __('创建时间'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Trip::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('user_id', __('用户ID'));
        $show->field('start_addr', __('开始城市'));
        $show->field('end_addr', __('结束城市'));
        $show->field('select_type', __('选择类型'));
        $show->field('trip_date', __('行程时间'));
        $show->field('trip_price', __('行程价格'));
        $show->field('trip_num', __('行程人数'));
        $show->field('car_code', __('车牌号'));
        $show->field('is_charter', __('是否包车'));
        $show->field('contacter', __('联系人'));
        $show->field('moblie', __('联系电话'));
        $show->field('remark', __('备注'));
        $show->field('created_at', __('创建时间'));
        //$show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Trip());

        $form->number('user_id', __('用户ID'));
        $form->text('start_addr', __('开始城市'))->required();
        $form->text('end_addr', __('结束城市'))->required();
        $form->radio('select_type', __('选择类型'))->options(['1' => '人找车', '2'=> '车找人'])->default('1');
        $form->datetime('trip_date', __('行程时间'))->default(date('Y-m-d H:i:s'))->required();
        $form->decimal('trip_price', __('行程价格'))->required();
        $form->number('trip_num', __('行程人数'))->required();
        $form->text('car_code', __('车牌号'))->required();
        //$form->number('is_charter', __('是否包车'))->default(1)->required();
        $form->radio('is_charter', __('是否包车'))->options(['1' => '是', '0'=> '否'])->default('1');
        $form->radio('status', __('状态'))->options(['1' => '通过审核', '0'=> '待审核', '2'=> '审核不通过'])->default('1');
        $form->text('contacter', __('联系人'))->required();
        $form->text('moblie', __('联系电话'))->required();
        $form->text('remark', __('备注'));

        return $form;
    }
}
